package hzk.codex2016.tree;

public class Easy {
	/**
	 * https://leetcode.com/problems/same-tree/
	 * 
	 * @param p
	 * @param q
	 * @return
	 */
	public boolean isSameTree(TreeNode p, TreeNode q) {
		if (p == null && q == null)
			return true;
		if (p == null && q != null || p != null && q == null)
			return false;
		return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);

	}

	/**
	 * https://leetcode.com/problems/symmetric-tree/
	 * 
	 * @param root
	 * @return
	 */
	public boolean isSymmetric(TreeNode root) {
		class S {
			boolean check(TreeNode lt, TreeNode rt) {
				if (lt == null)
					return rt == null;
				if (rt == null)
					return lt == null;
				return lt.val == rt.val && check(lt.left, rt.right) && check(lt.right, rt.left);
			}
		}
		S s = new S();
		return root == null || s.check(root.left, root.right);
	}

	public static void main(String[] args) {
		Easy me = new Easy();
		System.out.println(me.isSymmetric(Trees.from(1, 2, 2, -1, 3, -1, 3)));
		System.out.println(me.isSymmetric(Trees.from(1, 2, 2, -1, 3, 3, -1)));
	}
}
